package pers.cz.controller;

import pers.cz.date.DateUtils;
import pers.cz.model.NotifyMessage;
import pers.cz.pub.MyThreadLocals;
import pers.cz.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消息处理
 * @program: PostGirl-panent
 * @description: MessageController
 * @author: Cheng Zhi
 * @create: 2021-10-07 22:15
 **/
@RestController
@Api(tags = "消息通知接口")
@RequestMapping(path = "/api/NoitfyMessage")
public class MessageController {

    @Autowired
    MessageService service;

    @ApiOperation(value = "查询未读消息总量")
    @PostMapping(path = "quertNotifyMessageCount")
    public Integer quertNotifyMessageCount(@RequestBody Map map, HttpServletRequest request) {

        MyThreadLocals.myThreadInterface.set("quertNotifyMessageCount");
        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        String type = (String) map.get("type");
        Integer count = service.quertNotifyMessageCount(userId,type);
        return count;
    }

    @ApiOperation(value = "分页查询所有消息")
    @PostMapping(path = "queryAllNotifyMessage")
    public List<NotifyMessage> queryAllNotifyMessage(@RequestBody Map map, HttpServletRequest request) {

        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");
        return service.queryAllNotifyMessage(userId, pageStart, pageLimit);
    }


    @ApiOperation(value = "根据已读未读分页查询所有消息")
    @PostMapping(path = "queryAllNotifyMessageByType")
    public List<NotifyMessage> queryAllNotifyMessageByType(@RequestBody Map map, HttpServletRequest request) {

        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        String type = (String) map.get("type");
        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");
        return service.queryAllNotifyMessageByType(userId, type, pageStart, pageLimit);
    }

    @ApiOperation(value = "根据消息类型分页查询所有消息")
    @PostMapping(path = "queryAllNotifyMessageByMessageType")
    public List<NotifyMessage> queryAllNotifyMessageByMessageType(@RequestBody Map map, HttpServletRequest request) {

        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        String messageType = (String) map.get("messageType");
        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");
        return service.queryAllNotifyMessageByMessageType(userId, messageType, pageStart, pageLimit);
    }

    @ApiOperation(value = "修改消息类型为已读")
    @PostMapping(path = "updateNotifyMessageType")
    public Integer updateNotifyMessageType(@RequestBody Map map) {

        int id = (int) map.get("id");
        String type = (String) map.get("type");
        return service.updateNotifyMessageType(null, id, type);
    }

    @ApiOperation(value = "修改消息类型")
    @PostMapping(path = "updateNotifyMessageMessageType")
    public Integer updateNotifyMessageMessageType(@RequestBody Map map) {

        int id = (int) map.get("id");
        String messageType = (String) map.get("messageType");
        return service.updateNotifyMessageMessageType(null, id, messageType);
    }

    @ApiOperation(value = "删除消息")
    @PostMapping(path = "deleteMessageById")
    public Integer deleteMessageById(@RequestBody Map map) {

        int id = (int) map.get("id");
        return service.deleteMessageById(id);
    }

    @ApiOperation(value = "根据条件查询消息")
    @PostMapping(path = "queryNotifyMessagetByCondition")
    public List<NotifyMessage> queryNotifyMessagetByCondition(@RequestBody Map map, HttpServletRequest request) {

        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        String messageType = (String) map.get("messageType");
        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");
        String theme = (String) map.get("theme");

        Date startDate = null;
        Date endDate = null;
        try {
            startDate = DateUtils.convertTimeFormat((String) map.get("startDate"));
            endDate = DateUtils.convertTimeFormat((String) map.get("endDate"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String type = (String) map.get("type");

        return service.queryNotifyMessagetByCondition(userId, messageType, pageStart, pageLimit, type, startDate, endDate, theme);
    }

    @ApiOperation(value = "修改消息类型")
    @PostMapping(path = "updateNotifyMessageMessageTypeAll")
    public Integer updateNotifyMessageMessageTypeAll(@RequestBody Map map, HttpServletRequest request) {

        int userId = Integer.valueOf(request.getAttribute("userId").toString());
        String messageType = (String) map.get("type");
        return service.updateNotifyMessageTypeAll(userId, messageType);
    }


}
